<template>
  <div style="height:240px;overflow:hidden;margin-top: 5px ">
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span>未读邮件({{count}})</span>
        <div style="float: right; padding: 3px 0" type="text">
          <a @click="showDetail()">查看全部 &gt;</a>
        </div>
      </div>
      <el-table :show-header="false" :data="mails" class="left_menu_side">
        <el-table-column prop="Subject" :show-overflow-tooltip="true" label="主题">
          <template slot-scope="scope">
            <a @click="showSingle(scope.row)">{{scope.row.Subject}}</a>
          </template>
        </el-table-column>
        <el-table-column prop="From" label="接收人" width="70"></el-table-column>
        <el-table-column prop="Received" label="接收时间" :show-overflow-tooltip="true" width="148"></el-table-column>
      </el-table>
    </el-card>
  </div>

</template>

<script type="text/javascript">
  import { ACCESS_TOKEN } from '@/store/mutation-types'
  import Vue from 'vue'
  import { openIFrame } from '@/utils/util'

  export default {
    name: 'MailForm',
    data() {
      return {
        timer: '',
        count: 0,
        mails: []
      }
    },
    mounted() {
      this.getMailList();
      //每5分钟刷新一次
      this.timer = setInterval(this.getMailList, 300000);
    },
    beforeDestroy() {
      clearInterval(this.timer);
    },
    methods: {
      showSingle(record) {
        openIFrame(record.Subject, '/Mail/' + record.Subject, record.Subject, record.Url)
        this.$router.push({ name: record.Subject })
        this.getMailList();
      },
      showDetail() {
        let ticket = Vue.ls.get(ACCESS_TOKEN)
        let parmas = {
          fid:3,
          order:"date",
          desc:true,
        }
        let ReturnUrl = encodeURIComponent('module=mbox.ListModule|'+JSON.stringify(parmas))
        let url = process.env.VUE_APP_MAIL_API + '/login.aspx?ticket=' + ticket + '&hash=' + ReturnUrl
        openIFrame("MailList", '/MailList', '未读邮件', url)
        this.$router.push({ name: "MailList" })
      },
      getMailList() {
        let that = this
        let params = {
          serviceName: 'Xinfo.Bus.BL.Service.EgovPlatform.PortalLet.MailPortalLetService,Xinfo.Bus.BL.Service',
          guid: 'rcgz_wdyj',
          getCount: true,
          intTypeId: 0,
          strBizType: '',
          ticket: Vue.ls.get(ACCESS_TOKEN)
        }
        this.getRequest('/PortalLet/GetPortalLetData', params)
          .then(res => {
            that.mails = []
            res.data.forEach((row, index) => {
              if (index < 5) {
                that.mails.push(row)
              }
            })
            that.count = res.data.length
          })
      }
    }
  }
</script>